import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/tools/gwGetManager.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';

class SliverRouteStickyHeader extends StatefulWidget {
  SliverRouteStickyHeader({Key? key}) : super(key: key);

  @override
  State<SliverRouteStickyHeader> createState() =>
      _sliverRouteGWSectionHeaderState();
}

class _sliverRouteGWSectionHeaderState extends State<SliverRouteStickyHeader> {
  @override
  Widget build(BuildContext context) {
    return getNolmalBody();
  }

  Widget getNolmalBody() {
    return Scaffold(
      appBar: AppBar(
        title: Text(RouteName.sliverRoute_stickyHeader),
      ),
      body: Container(
        child: ListView.builder(
                itemCount: 10,
                itemBuilder: (context, index) {
                  if (index < 2) {
                    return StickyHeader(
                      header: Container(
                        height: 50.0,
                        color: Colors.blueGrey[700],
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          'Header ',
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                      content: Container(
                        child: Expanded(
                          child: Image.asset("imgs/fengjing.png"),
                        ),
                      ),
                    );
                  } else if (index < 4) {
                    return StickyHeaderBuilder(
                      // controller: controller, // Optional
                      builder: (BuildContext context, double stuckAmount) {
                        stuckAmount = 1.0 - stuckAmount.clamp(0.0, 1.0);
                        return Container(
                          height: 50.0,
                          color: Color.lerp(
                              Colors.blue[700], Colors.red[700], stuckAmount),
                          padding: const EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.centerLeft,
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                child: Text(
                                  'Header #$index',
                                  style: const TextStyle(color: Colors.white),
                                ),
                              ),
                              Offstage(
                                offstage: stuckAmount <= 0.0,
                                child: Opacity(
                                  opacity: stuckAmount,
                                  child: IconButton(
                                    icon: const Icon(Icons.favorite,
                                        color: Colors.white),
                                    onPressed: () =>
                                        ScaffoldMessenger.of(context)
                                            .showSnackBar(SnackBar(
                                                content:
                                                    Text('Favorite #$index'))),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                      content: Container(
                        child: Expanded(
                          child: Image.asset("imgs/fengjing.png"),
                        ),
                      ),
                    );
                  } else {
                    return StickyHeaderBuilder(
                      overlapHeaders: true,
                      // controller: controller, // Optional
                      builder: (BuildContext context, double stuckAmount) {
                        stuckAmount = 1.0 - stuckAmount.clamp(0.0, 1.0);
                        return Container(
                          height: 50.0,
                          color: Colors.grey.shade900
                              .withOpacity(0.6 + stuckAmount * 0.4),
                          padding: const EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.centerLeft,
                          child: Text(
                            'Header #$index',
                            style: const TextStyle(color: Colors.white),
                          ),
                        );
                      },
                      content: Container(
                        child: Expanded(
                          child: Image.asset("imgs/fengjing.png"),
                        ),
                      ),
                    );
                  }
                }),
      ),
    );
  }

  Widget headerSectionBuilder(
      BuildContext context, double maxHeight, bool fixed) {
    var theme = Theme.of(context);
    return Material(
      elevation: fixed ? 4 : 0,
      shadowColor: theme.appBarTheme.shadowColor,
      child: Container(
        color: fixed ? Colors.white : Colors.red,
        child: headerSectionTitle("one"),
      ),
    );
  }

  Widget headerSectionTitle(String text) {
    return ListTile(
      title: Text(text),
      onTap: () => print("ListTile - $text"),
    );
  }
}
